package org.hdax.dm.service.impl;

import org.hda.dm.utils.DateUtil;
import org.hdax.dm.client.*;
import org.hdax.dm.entity.cinema.DmCinema;
import org.hdax.dm.entity.es.item.Dmitem;
import org.hdax.dm.entity.image.DmImage;
import org.hdax.dm.entity.item.DmItem;
import org.hdax.dm.entity.item_type.DmItemType;
import org.hdax.dm.entity.scheduler.DmScheduler;
import org.hdax.dm.request.QueryItemLike;
import org.hdax.dm.request.QueryItemListRequest;
import org.hdax.dm.response.CommonPageRespone;
import org.hdax.dm.service.DmItemService;
import org.hdax.dm.vo.CommonResponse;
import org.hdax.dm.vo.VoUtil;
import org.hdax.dm.vo.entity.item.*;
import org.hdax.dm.vo.entity.scheduler.VoScheduler;
import org.hdax.dm.vo.es.item.VoEsDmItem;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.Criteria;
import org.springframework.data.elasticsearch.core.query.CriteriaQuery;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class DmItemServiceImpl implements DmItemService {

    @Autowired
    private ElasticsearchTemplate esTemplate;

    @Autowired
    private DmImageClient dmImageClient;

    @Autowired
    private DmItemClient dmItemClient;

    @Autowired
    private DmItemTypeClient dmItemTypeClient;

    @Autowired
    private DmCinemaClient dmCinemaClient;

    @Autowired
    private DmSchedulerClient dmSchedulerClient;

    @Override
    public CommonResponse<List<VoDmItem>> queryBanner() {
        List<DmImage> dmImageList = dmImageClient.findByImage(new DmImage(null, 1, 1));
        List<VoDmItem> voDmItemList = new ArrayList<>();
        dmImageList.forEach(item -> {
            if (item.getTargetId() != null) {
                DmItem dmItem = dmItemClient.findById(item.getTargetId());
                VoDmItem voDmItem = new VoDmItem();
                BeanUtils.copyProperties(dmItem, voDmItem);
                voDmItem.setImgUrl(item.getImgUrl());
                voDmItemList.add(voDmItem);
            }
        });
        return VoUtil.returnSuccess(voDmItemList);
    }

    @Override
    public CommonResponse<List<VoDmItem>> queryTodayRecommend() {
        List<DmItem> dmItemList = dmItemClient.findByDmitem(0, 6, new DmItem(1, new Date()));
        List<VoDmItem> voDmItemList = new ArrayList<>();
        dmItemList.forEach(item -> {
            VoDmItem voDmItem = new VoDmItem();
            BeanUtils.copyProperties(item, voDmItem);
            List<DmImage> dmImages = dmImageClient.findByImage(new DmImage(item.getId(), 2, 1));
            if (dmImages.size() > 0) {
                voDmItem.setImgUrl(dmImages.get(0).getImgUrl());
            }
            voDmItemList.add(voDmItem);
        });
        return VoUtil.returnSuccess(voDmItemList);
    }

    @Override
    public CommonResponse<List<VoDmItems>> queryToSaleItem() {
        List<VoDmItems> voDmItemsList = new ArrayList<>();
        List<DmItemType> dmItemTypeList = dmItemTypeClient.findByParent(0);
        dmItemTypeList.forEach(item -> {
            VoDmItems voDmItems = new VoDmItems();
            voDmItems.setItemTypeName(item.getItemType());
            voDmItems.setItemTypeId(item.getId());
            voDmItems.setFloor(voDmItemsList.size() + 1);

            List<VoItems> voItemsList = new ArrayList<>();
            //遍历一级菜单的热门推荐
            List<DmItem> dmItemList = dmItemClient.findByDmitem(0, 5, new DmItem(item.getId(), 1));
            dmItemList.forEach(items -> {
                if (items != null && item.getId() == items.getItemType1Id()) {
                    //向客户端返回的节目数据进行替换
                    VoItems voItems = new VoItems();
                    List<DmImage> dmImages = dmImageClient.findByImage(new DmImage(items.getId(), 2, 1));
                    if (dmImages.size() > 0) {
                        voItems.setImgUrl(dmImages.get(0).getImgUrl());
                    }
                    voItems.setStartDate(DateUtil.dateToString(items.getStartTime()));
                    voItems.setEndDate(DateUtil.dateToString(items.getEndTime()));
                    BeanUtils.copyProperties(items, voItems);
                    voItemsList.add(voItems);
                }
            });
            voDmItems.setItems(voItemsList);
            voDmItemsList.add(voDmItems);
        });
        return VoUtil.returnSuccess(voDmItemsList);
    }

    @Override
    public CommonResponse<VoQueryItemDetail> queryItemDetail(Integer id) {
        DmItem dmItem = dmItemClient.findById(id);
        VoQueryItemDetail voQueryItemDetail = new VoQueryItemDetail();
        BeanUtils.copyProperties(dmItem, voQueryItemDetail);
        List<DmImage> dmImages = dmImageClient.findByImage(new DmImage(dmItem.getId(), 2, 1));
        if (dmImages.size() > 0) {
            voQueryItemDetail.setImgUrl(dmImages.get(0).getImgUrl());
        }
        voQueryItemDetail.setStartTime(DateUtil.dateToString(dmItem.getStartTime()));
        voQueryItemDetail.setEndTime(DateUtil.dateToString(dmItem.getEndTime()));
        voQueryItemDetail.setState(dmItem.getState() + "");
        return VoUtil.returnSuccess(voQueryItemDetail);
    }

    @Override
    public CommonResponse<List<VoQueryItemRecommend>> queryItemRecommend(Integer itemTypeId) {
        List<VoQueryItemRecommend> voQueryItemRecommendList = new ArrayList<>();
        List<DmItem> dmItemList = dmItemClient.findByDmitem(0, 10, new DmItem(itemTypeId, 1));

        dmItemList.forEach(item -> {
            VoQueryItemRecommend voQueryItemRecommend = new VoQueryItemRecommend();
            BeanUtils.copyProperties(item, voQueryItemRecommend);
            List<DmImage> dmImages = dmImageClient.findByImage(new DmImage(item.getId(), 2, 1));
            if (dmImages.size() > 0) {
                voQueryItemRecommend.setImgUrl(dmImages.get(0).getImgUrl());
            }
            voQueryItemRecommend.setStartDate(DateUtil.dateToString(item.getStartTime()));
            voQueryItemRecommend.setEndDate(DateUtil.dateToString(item.getEndTime()));
            voQueryItemRecommendList.add(voQueryItemRecommend);
        });
        return VoUtil.returnSuccess(voQueryItemRecommendList);
    }

    @Override
    public CommonResponse<CommonPageRespone<List<VoEsDmItem>>> queryItemList(QueryItemListRequest queryItemListRequest) {
        Criteria criteria = new Criteria();
        CriteriaQuery criteriaQuery = new CriteriaQuery(criteria);
        if (queryItemListRequest != null) {
            if (queryItemListRequest.getAreaId() != null && queryItemListRequest.getAreaId() > 0)
                criteria.and(new Criteria("areaid").is(queryItemListRequest.getAreaId()));
            if (queryItemListRequest.getItemTypeId1() != null && queryItemListRequest.getItemTypeId1() > 0)
                criteria.and(new Criteria("itemtype1id").is(queryItemListRequest.getItemTypeId1()));
            if (queryItemListRequest.getItemTypeId2() != null && queryItemListRequest.getItemTypeId2() > 0)
                criteria.and(new Criteria("itemtype2id").is(queryItemListRequest.getItemTypeId2()));
            if (queryItemListRequest.getKeyword() != null)
                criteria.and(new Criteria("itemname").contains(queryItemListRequest.getKeyword()));

            if (queryItemListRequest.getStartTime() != null && !queryItemListRequest.getStartTime().equals(""))
                criteria.and(new Criteria("starttime").greaterThanEqual(queryItemListRequest.getStartTime()));
            if (queryItemListRequest.getEndTime() != null && !queryItemListRequest.getStartTime().equals(""))
                criteria.and(new Criteria("starttime").lessThanEqual(queryItemListRequest.getEndTime()));
            if (queryItemListRequest.getSort() != null && queryItemListRequest.getSort().equals("recommend"))
                criteria.and(new Criteria("isrecommend").is(1));
            if (queryItemListRequest.getSort() != null && queryItemListRequest.getSort().equals("recentShow"))
                criteriaQuery.addSort(new Sort(Sort.Direction.DESC, "starttime"));
            if (queryItemListRequest.getSort() != null && queryItemListRequest.getSort().equals("recentSell"))
                criteriaQuery.addSort(new Sort(Sort.Direction.DESC, "createdtime"));
        }

        criteriaQuery.setPageable(PageRequest.of(queryItemListRequest.getCurrentPage(), queryItemListRequest.getPageSize()));
        queryItemListRequest.setTotal((int) esTemplate.count(criteriaQuery, Dmitem.class));

        List<Dmitem> dmitemList = esTemplate.queryForPage(criteriaQuery, Dmitem.class).getContent();
        List<VoEsDmItem> voEsDmItemList = new ArrayList<>();
        CommonPageRespone<List<VoEsDmItem>> commonResponse = new CommonPageRespone<>();
        commonResponse.setCurrentPage(queryItemListRequest.getCurrentPage());
        commonResponse.setPageCount(queryItemListRequest.getPageCount());
        commonResponse.setPageSize(queryItemListRequest.getPageSize());
        commonResponse.setTotal(queryItemListRequest.getTotal());
        dmitemList.forEach(item -> {
            VoEsDmItem voEsDmItem = new VoEsDmItem();
            voEsDmItem.setId(item.getId());
            voEsDmItem.setAreaName(item.getAreaname());
            voEsDmItem.setItemName(item.getItemname());
            voEsDmItem.setAbstractMessage(item.getAbstractmessage());
            voEsDmItem.setStartTime(DateUtil.dateToString(item.getStarttime()));
            voEsDmItem.setEndTime(DateUtil.dateToString(item.getEndtime()));
            voEsDmItem.setMinPrice(item.getMinprice());
            voEsDmItem.setMaxPrice(item.getMaxprice());
            voEsDmItem.setAdress(item.getAdress());
            voEsDmItem.setLatitude(item.getLatitude());
            voEsDmItem.setLongitude(item.getLongitude());
            List<DmImage> dmImages = dmImageClient.findByImage(new DmImage(item.getId(), 2, 1));
            if (dmImages.size() > 0) {
                voEsDmItem.setImgUrl(dmImages.get(0).getImgUrl());
            }
            voEsDmItemList.add(voEsDmItem);
        });
        commonResponse.setRows(voEsDmItemList);

        return VoUtil.returnSuccess(commonResponse);
    }

    @Override
    public CommonResponse<List<VoQueryItemLike>> queryItemLike(QueryItemLike queryItemLike) {
        List<DmItem> dmitemList = null;

        List<DmItemType> dmItemTypeList = dmItemTypeClient.findByParent(0);
        if (queryItemLike.getItemTypeId() == 0) {
            dmitemList = dmItemClient.findByDmitem(0, 11, new DmItem(dmItemTypeList.get(0).getId(), 1));
        } else {
            if (queryItemLike.getItemTypeId() <= dmItemTypeList.get(dmItemTypeList.size() - 1).getId()) {
                dmitemList = dmItemClient.findByDmitem(0, 11, new DmItem(queryItemLike.getItemTypeId(), 1));
            } else {
                DmItem dmitem = new DmItem();
                dmitem.setItemType2Id(queryItemLike.getItemTypeId());
                dmitem.setIsBanner(1);
                dmitemList = dmItemClient.findByDmitem(0, 11, dmitem);
            }
        }
        List<VoQueryItemLike> voQueryItemLikeArrayList = new ArrayList<>();
        dmitemList.forEach(item -> {
            VoQueryItemLike voQueryItemLike = new VoQueryItemLike();
            voQueryItemLike.setId(item.getId());
            voQueryItemLike.setItemName(item.getItemName());
            List<DmCinema> dmCinemaList = dmCinemaClient.findByDmCinema(new DmCinema(item.getCinemaId()));
            if (dmCinemaList.size() > 0) {
                voQueryItemLike.setAreaId(dmCinemaList.get(0).getAreaId());
                voQueryItemLike.setAreaName(dmCinemaList.get(0).getAreaName());
                voQueryItemLike.setAddress(dmCinemaList.get(0).getAddress());
            }
            voQueryItemLike.setStartDate(DateUtil.dateToString(item.getStartTime()));
            voQueryItemLike.setEndDate(DateUtil.dateToString(item.getEndTime()));
            List<DmImage> dmImages = dmImageClient.findByImage(new DmImage(item.getId(), 2, 1));
            if (dmImages.size() > 0) {
                voQueryItemLike.setImgUrl(dmImages.get(0).getImgUrl());
            }

            voQueryItemLike.setMinPrice(item.getMinPrice());
            voQueryItemLikeArrayList.add(voQueryItemLike);
        });
        return VoUtil.returnSuccess(voQueryItemLikeArrayList);
    }

}
